import request from '../utils/request'
let index = {
    namespace: 'chat',
    state: {
        chatMsg:[],
        unread:0,
        from:'',
    },
    effects:{
        *getChatList({payload},{call,put}){
            let res = yield call(request('/getChatList'));
            //console.log(res);
            yield put({type:'initChatData',payload:{data:res.data.data,userid:res.data.userid}})
        },

    },
    reducers:{
        addItem(state,{payload}){
            return{
                chatMsg:[...state.chatMsg,payload.data],
                unread:state.unread+payload.n,
            }
        },
        initChatData(state,{payload}){
            //console.log(state);
            return{
                chatMsg:[...state.chatMsg,...payload.data],
                unread:payload.data.filter(v=>!v.read&&v.to == payload.userid).length,
            }
        },
       
    }
}
export default index;